
package view;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import model.core.torneo.Torneo;
import model.core.torneo.calendario.algorithm.FactoryCalendarGenerator;
import model.core.utente.Commissario;
import model.utility.Utility;

/**
 * 
 * @author Giuseppe Fusco (fusgius@gmail.com)
 *
 */
public class IscrizioneTorneoSingolo extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1464501289843989312L;

	private String step1, step2, step3, step4, step5;

	private CaratteristicheTorneo caratteristichePanel;
	private PartecipantiSingoliTorneoVistaLabel partecipantiGlobale;
	private SelezioneCommissariTorneo selezionaCommissari;
	private CalendarioTorneo calendarioTorneo;
	private JTextArea riepilogo;
	private int currentStatus;
	private Torneo torneo;

	public IscrizioneTorneoSingolo(Commissario com) {
		initComponents();
		step1 = "Definisci le caratteristiche del torneo";
		step2 = "Seleziona i partecipanti del torneo";
		step3 = "Seleziona i commissari del torneo";
		step4 = "Riepologo. Controlla i dati inseriti e scegli se confermare o annullare";
		step5 = "Elenco delle gare per il torneo";
		caratteristichePanel = new CaratteristicheTorneo(com);
		partecipantiGlobale = new PartecipantiSingoliTorneoVistaLabel(null);
		selezionaCommissari= new SelezioneCommissariTorneo();
		calendarioTorneo = new CalendarioTorneo();
		riepilogo = new JTextArea();
		operationStatus.setText(step1);
		mainScroll.setViewportView(caratteristichePanel);
		currentStatus = 1;
		indietro.setEnabled(false);
		addListener();
	}

	private void addListener(){
		continua.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				switch (currentStatus) {
				case 1:
					torneo = caratteristichePanel.getTorneo();
					if(torneo != null){
						partecipantiGlobale.setTorneo(torneo);
						operationStatus.setText(step2);
						mainScroll.setViewportView(partecipantiGlobale);
						indietro.setEnabled(true);
						currentStatus = 2;
					}
					break;
				case 2:
					int size = torneo.getClassifica().getSize();
					if(size==torneo.getMaxComponenti()){
						operationStatus.setText(step3);
						selezionaCommissari.setTorneo(torneo);
						mainScroll.setViewportView(selezionaCommissari);
						currentStatus = 3;
					}else
						JOptionPane.showMessageDialog(getParent(), "Non hai raggiunto il numero massimo di partecipanti.");
					break;
				case 3:
					operationStatus.setText(step4);
					riepilogo.setText(Utility.toStringTorneo(torneo).toString());
					mainScroll.setViewportView(riepilogo);
					currentStatus = 4;
					break;
				case 4:
					operationStatus.setText(step5);
					torneo.getCalendario().setAll(FactoryCalendarGenerator.getGironeItaliano(torneo.getClassifica().getAll()));
					calendarioTorneo.setTorneo(torneo);
					mainScroll.setViewportView(calendarioTorneo);
					currentStatus = 5;
					break;
				default:
					break;
				}
			}
		});

		indietro.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				switch (currentStatus) {
				case 2:
					torneo.getClassifica().removeAll();
					operationStatus.setText(step1);
					mainScroll.setViewportView(caratteristichePanel);
					currentStatus = 1;
					break;
				case 3:
					torneo.getCommissione().removeAll();
					operationStatus.setText(step2);
					mainScroll.setViewportView(partecipantiGlobale);
					currentStatus = 2;
					break;
				case 4:
					operationStatus.setText(step3);
					mainScroll.setViewportView(selezionaCommissari);
					currentStatus = 3;
					break;
				case 5:
					operationStatus.setText(step4);
					mainScroll.setViewportView(selezionaCommissari);
					currentStatus = 4;
					break;
				default:
					break;
				}
			}
		});
		
		annulla.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}


	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jPanel2 = new javax.swing.JPanel();
		operationStatus = new javax.swing.JLabel();
		mainScroll = new javax.swing.JScrollPane();
		jPanel1 = new javax.swing.JPanel();
		annulla = new javax.swing.JButton();
		indietro = new javax.swing.JButton();
		continua = new javax.swing.JButton();

		operationStatus.setFont(new java.awt.Font("Pericles", 0, 12)); // NOI18N
		operationStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		operationStatus.setText("jLabel1");

		annulla.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		annulla.setText("annulla");
		jPanel1.add(annulla);

		indietro.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		indietro.setText("indietro");
		jPanel1.add(indietro);

		continua.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		continua.setText("continua");
		jPanel1.add(continua);

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(
				jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPanel2Layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(operationStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(mainScroll)
								.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 720, Short.MAX_VALUE))
								.addContainerGap())
				);
		jPanel2Layout.setVerticalGroup(
				jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPanel2Layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(operationStatus)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(mainScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap())
				);

		jScrollPane1.setViewportView(jPanel2);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jScrollPane1)
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jScrollPane1)
				);
	}

	private javax.swing.JButton annulla;
	private javax.swing.JButton continua;
	private javax.swing.JButton indietro;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane mainScroll;
	private javax.swing.JLabel operationStatus;
}
